/**
 * 参数过滤
 * @param obj json 参数
 * @return 过滤掉为空的参数
 * @author: herther
 * @since 1.0.0
 * @date: 2022/9/12 14:38
 */
const removeEmpty = (obj) => {
  Object.keys(obj).forEach((k, v) => {
    v = obj[k];
    if (v === '') delete obj[k];
    else if (v.constructor == Object) removeEmpty(v);
  });
  return obj;
};

/**
 * 构建分页参数
 * @param {查询参数} queryParams
 * @param {排序参数} sorts
 */
const buildParams = (queryParams, sorts) => {
  queryParams['page'] = queryParams.current;
  delete queryParams['current'];
  queryParams['sorted'] = buildSorted(sorts);
  // 移除空的参数
  removeEmpty(queryParams);
  return queryParams;
};

/**
 * 构建排序
 */
const buildSorted = (sorts) => {
  let sortStr = '';
  Object.keys(sorts).forEach((k, v) => {
    v = sorts[k];
    let tempRule;
    if (v.indexOf('desc') == 0) {
      tempRule = 'desc';
    } else {
      tempRule = 'asc';
    }
    sortStr = k + ':' + tempRule;
  });
  return sortStr;
};

export { removeEmpty, buildParams, buildSorted };
